﻿using MachineVision.API.Models;
using MachineVision.Service;
using MachineVision.Service.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Drawing;
using System.Drawing.Printing;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace MachineVision.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WarningController : ControllerBase
    {
        private readonly ILogger<WarningController> logger;
        private readonly IMissionService mService;

        public WarningController(ILogger<WarningController> logger, IMissionService mService)
        {
            this.logger = logger;
            this.mService = mService;
        }


        // GET: api/<MissionHistoryController>
        [HttpGet]
        public ApiResult Get(int pageNum = 1, int pageSize = 10, DateTime? TimeBegin = null, DateTime? TimeEnd = null)
        {
            ApiResult result = new ApiResult();
            var list= mService.GetWarnings(out int total,pageNum, pageSize, TimeBegin, TimeEnd);
            list.ForEach(s => { 
                if (!string.IsNullOrEmpty(s.VisionFile)) 
                {
                    try
                    {
                        s.VisionFile = MyTools.Imagetoase64(new Bitmap(s.VisionFile));
                    }
                    catch (Exception)
                    {
                    }
                }
            });
            result.data = list;
            result.total = total;
            result.page = pageNum;
            result.count = pageSize;
            return result;
        }

        // GET api/<MissionHistoryController>/5
        [HttpGet("{id}")]
        public async Task<ApiResult> Get(int id)
        {
            ApiResult result = new ApiResult();
            result.data = mService.GetWarning(id);
            return result;
        }

        // POST api/<MissionHistoryController>
        [HttpPost]
        public ApiResult Post([FromBody] Warning value)
        {
            ApiResult result = new ApiResult();

            if (!string.IsNullOrEmpty(value.VisionFile))
                value.VisionFile = MyTools.Base64toImage(value.VisionFile);
            result.data = mService.AddWarning(value);
            return result;
        }



        // PUT api/<MissionHistoryController>/5
        //[HttpPut("{id}")]
        //public void Put(int id, [FromBody] string value)
        //{
        //}

        //// DELETE api/<MissionHistoryController>/5
        //[HttpDelete("{id}")]
        //public void Delete(int id)
        //{
        //}
    }
}
